﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Phone.Tasks;

namespace PhysicsSimulator
{
    public partial class MainPage : Microsoft.Phone.Controls.PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;

        UIElementRenderer elementRenderer;

        List<CDotLight> _lights = new List<CDotLight>();

        CSprite _background = null;

        bool _loaded = false;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            AudioManager.Initialize((Application.Current as App).Content);
            AudioManager.LoadSounds();

            contentManager = (Application.Current as App).Content;

            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += timer_Update;
            timer.Draw += timer_Draw;

            LayoutUpdated += MainPage_LayoutUpdated;
        }

        void MainPage_LayoutUpdated(object sender, EventArgs e)
        {
            // make sure page size is valid
            if (ActualWidth == 0 || ActualHeight == 0)
                return;

            // see if we already have the right sized renderer
            if (elementRenderer != null &&
                elementRenderer.Texture != null &&
                elementRenderer.Texture.Width == (int)ActualWidth &&
                elementRenderer.Texture.Height == (int)ActualHeight)
            {
                return;
            }

            // dispose the current renderer
            if (elementRenderer != null)
                elementRenderer.Dispose();

            // create the renderer
            elementRenderer = new UIElementRenderer(this, (int)ActualWidth, (int)ActualHeight);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            if (!_loaded)
            {
                for (int i = 0; i < 8; i++)
                {
                    CDotLight light = new CDotLight(spriteBatch, i);
                    light.Init(contentManager);
                    light.RandomRirection();
                    _lights.Add(light);
                }
                _loaded = true;
            }
            else
            {
                for (int i = 0; i < 8; i++)
                {
                    _lights[i].SpriteBatch = spriteBatch;
                }
            }

            _background = new CSprite(contentManager.Load<Texture2D>("Images/DotLight/background"));

            timer.Start();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            timer.Stop();
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        private void TextBlock_Tap(object sender, GestureEventArgs e)
        {
            SinglePendulumSettings.RenewTempData();
            NavigationService.Navigate(new Uri("/SinglePendulum/SinglePendulumPage.xaml", UriKind.Relative));
        }

        private void TextBlock_Tap_1(object sender, GestureEventArgs e)
        {
            Settings.RenewSpringsDatas();
            NavigationService.Navigate(new Uri("/SpringsPendulum/SpringsPendulumPage.xaml", UriKind.Relative));
        }

        private void MMQuanTinh_Tap(object sender, GestureEventArgs e)
        {
            Settings.RenewTempData();
            NavigationService.Navigate(new Uri("/Moments/TurnablePage.xaml", UriKind.Relative));
        }

        private void TextBlock_Tap_2(object sender, GestureEventArgs e)
        {
            ThrowObliqueSettings.RenewTempData();
            NavigationService.Navigate(new Uri("/ThrowOblique/ThrowObliquePage.xaml", UriKind.Relative));
        }

        private void CatapultGame_Tap(object sender, GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Game/MayBanDa/CatapultGameplay.xaml?msg=0", UriKind.Relative));
        }

        private void Candy_Tap(object sender, GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Game/DapKeo/CandyGamePage.xaml", UriKind.Relative));
        }

        private void TracNghiem_Tap(object sender, GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Game/AiLaTrieuPhu/StartGamePage.xaml", UriKind.Relative));
        }

        void timer_Update(object sender, GameTimerEventArgs e)
        {
            foreach(CDotLight light in _lights)
                light.Update(e);
        }

        void timer_Draw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.White);
            elementRenderer.Render();
            spriteBatch.Begin();
            spriteBatch.Draw(_background.Texture, new Vector2(240, 400), null, Color.White, 0, _background.Origin, 1, SpriteEffects.None, 0);
            foreach (CDotLight light in _lights)
                light.Draw(e);
            spriteBatch.Draw(elementRenderer.Texture, Vector2.Zero, Color.White);            
            spriteBatch.End();
        }

        private void gioithieu_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
        }

        private void danhgia_Click(object sender, EventArgs e)
        {
            MarketplaceReviewTask review = new MarketplaceReviewTask();
            review.Show();
        }

        private void lienhe_Click(object sender, EventArgs e)
        {
            EmailComposeTask emailComposeTask = new EmailComposeTask();

            emailComposeTask.Subject = "Góp ý Ứng dụng mô phỏng vật lý!";           
            emailComposeTask.To = "uit-dev@live.com";          

            emailComposeTask.Show();
        }
    }
}